library(zoo)
library(xts)
#smooth the data 
smoothData <- function(timeSeries, MeanDepth, IndexTimeSeries) {
  ret <- as.vector(rollapply(timeSeries, MeanDepth, mean, align="right"))
  return(as.xts(ret,order.by=IndexTimeSeries[MeanDepth:length(IndexTimeSeries)]))
}

szf <- smoothData(zf,4,IndexZf);
logSzf <- log(szf)
plot(logSzf)

plot(log(as.vector(zf[2:length(zf)])/as.vector(zf[1:length(zf)-1])),type="l")
plot(diff(zf))
spectrum(szf,log="no")
spec.ar(zf,order=12,plot=TRUE)

data(AirPassengers)
plot(AirPassengers)
plot(log10(AirPassengers))
AirPassengers

(fit <- arima(log10(AirPassengers), c(0, 1, 1),
              seasonal = list(order=c(0, 1 ,1), period=12)))
update(fit, method = "CSS")
?update
?arima
update(fit, x=window(log10(AirPassengers), start = 1954))
pred <- predict(fit, n.ahead = 24)
pred
tl <- pred$pred - 1.96 * pred$se
tu <- pred$pred + 1.96 * pred$se
ts.plot(AirPassengers, 10^tl, 10^tu, log = "y", lty = c(1,2,2))

logSzf <-as.ts(logSzf)
(fit <- arima(logSzf[1:12], c(0, 1, 1),
              seasonal = list(order=c(0, 1 ,2), period=8)))
update(fit, method = "CSS")
update(fit, x=window(logSzf, start = 13))

pred <- predict(fit, n.ahead = 12)
tl <- xts(x=exp(1)^(pred$pred - 1.96 * pred$se),order.by=index(zf[13:24]))
tu <- xts(x=exp(1)^(pred$pred + 1.96 * pred$se),order.by=index(zf[13:24]))
pred <-xts(x=exp(1)^pred$pred, order.by=index(zf[13:24]))
plot(rbind(zf[1:12],pred), lty=4)
lines(zf)
lines(rbind(zf[1:12],tl), lty=2)
lines(rbind(zf[1:12],tu), lty=2)



